home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 37 / IOPROG_37.ISO / SOFT / Multilizer.exe / disk1 / data1.cab / data1 / [Group9]VCL Source Standard / ivdsmult.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1999-08-12  |  16.7 KB  |  634 lines

  1. unit IvDsMult;
  2.  
  3. {$I IVMULTI.INC}
  4.  
  5. interface
  6.  
  7. uses
  8.   Windows, SysUtils, Classes, IvSocket, IvDictio, IvMLTP, IvParser;
  9.  
  10. type
  11.   TIvServerDictionary = class(TIvDictionary)
  12.   protected
  13.     FTranslationMode: TIvTranslationMode;
  14.     FCodePage: Integer;
  15.     FRemoteDictionaryName: String;
  16.     FUserName: String;
  17.     FPassword: String;
  18.     FTimeout: Integer;
  19.     FSocket: TIvWinSocket;
  20.     FStream: TIvWinSocketStream;
  21.     FUserType: TIvUserType;
  22.  
  23.     function GetAddress: String;
  24.     procedure SetAddress(const value: String);
  25.  
  26.     function GetPort: Integer;
  27.     procedure SetPort( value: Integer);
  28.  
  29.     procedure SetRemoteDictionaryName(const value: String);
  30.  
  31.     function ReadMessage(stream: TIvWinSocketStream; timeout: Integer): String;
  32.     function ReadReply(timeout: Integer; raiseException: Boolean; var reply: String): Integer;
  33.  
  34.     function Transaction(const msg: String): String;
  35.     function TransactionEx(const msg: String; raiseException: Boolean; var reply: String): Integer;
  36.  
  37.     function GetTranslationCount: Integer; override;
  38.     function GetLanguageCount: Integer; override;
  39.     procedure GetLanguageData(index: Integer; language: TIvLanguage); override;
  40.     function GetLocaleCount: Integer; override;
  41.     procedure GetLocaleData(index: Integer; locale: TIvLocale); override;
  42.     procedure LanguageChanged(languageChanged, localeChanged: Boolean); override;
  43.  
  44.   public
  45.     constructor Create(owner: TComponent); override;
  46.     destructor Destroy; override;
  47.  
  48.     procedure Open; override;
  49.     procedure Close; override;
  50.  
  51.     function TranslateString(
  52.       const str: String;
  53.       var translation: String): Boolean; override;
  54.     function TranslateContextString(
  55.       const str, form, component: String;
  56.       var translation: String): Boolean; override;
  57.  
  58.     function GetTranslationMode: TIvTranslationMode; override;
  59.     procedure TranslateStrings(translations: TList); override;
  60.  
  61.     procedure Login;
  62.     procedure Logout;
  63.  
  64.     function GetDictionaries(names, owners, descriptions: TStrings): Integer;
  65.  
  66.     class procedure StringToLanguage(const str: String; language: TIvLanguage);
  67.     class procedure StringToLocale(const str: String; locale: TIvLocale);
  68.  
  69.     class function GetLoginMessage(
  70.       const userName, password: String;
  71.       clientType: TIvClientType;
  72.       const clientVersion: String;
  73.       application: TIvApplicationType;
  74.       codePage: Integer): String;
  75.  
  76.     property UserType: TIvUserType read FUserType;
  77.  
  78.   published
  79.     property CodePage: Integer read FCodePage write FCodePage default DEFAULT_CODE_PAGE_C;
  80.     property UserName: String read FUserName write FUserName;
  81.     property Password: String read FPassword write FPassword;
  82.     property RemoteDictionaryName: String read FRemoteDictionaryName write SetRemoteDictionaryName;
  83.     property Address: String read GetAddress write SetAddress;
  84.     property Port: Integer read GetPort write SetPort default DEFAULT_PORT_C;
  85.     property Timeout: Integer read FTimeout write FTimeout default DEFAULT_TIMEOUT_C;
  86.     property TranslationMode: TIvTranslationMode read FTranslationMode write FTranslationMode default ivtmMultiple;
  87.   end;
  88.  
  89. implementation
  90.  
  91. constructor TIvServerDictionary.Create(owner: TComponent);
  92. begin
  93.   inherited Create(owner);
  94.  
  95.   FTimeout := DEFAULT_TIMEOUT_C;
  96.   FCodePage := DEFAULT_CODE_PAGE_C;
  97.   FTranslationMode := ivtmMultiple;
  98.  
  99.   FSocket := TIvWinSocket.Create;
  100.   FSocket.Port := DEFAULT_PORT_C;
  101.   SetAddress('127.0.0.1');
  102. end;
  103.  
  104. destructor TIvServerDictionary.Destroy;
  105. begin
  106.   Close;
  107.   FSocket.Free;
  108.   inherited Destroy;
  109. end;
  110.  
  111. function TIvServerDictionary.GetAddress: String;
  112. begin
  113.   if FSocket.Host <> '' then
  114.     Result := FSocket.Host
  115.   else
  116.     Result := FSocket.Address;
  117. end;
  118.  
  119. procedure TIvServerDictionary.SetAddress(const value: String);
  120. begin
  121.   if value <> Address then
  122.   begin
  123.     if IvIsDNSAddress(value) then
  124.     begin
  125.       FSocket.Host := value;
  126.       FSocket.Address := '';
  127.     end
  128.     else
  129.     begin
  130.       FSocket.Address := value;
  131.       FSocket.Host := '';
  132.     end;
  133.   end;
  134. end;
  135.  
  136. function TIvServerDictionary.GetPort: Integer;
  137. begin
  138.   Result := FSocket.Port;
  139. end;
  140.  
  141. procedure TIvServerDictionary.SetPort( value: Integer);
  142. begin
  143.   if value <> Port then
  144.   begin
  145.     FSocket.Port := value;
  146.   end;
  147. end;
  148.  
  149. procedure TIvServerDictionary.SetRemoteDictionaryName(const value: String);
  150. begin
  151.   if value <> RemoteDictionaryName then
  152.   begin
  153.     FRemoteDictionaryName := value;
  154.     if IsOpen then
  155.       Open;
  156.   end;
  157. end;
  158.  
  159. function TIvServerDictionary.ReadMessage(
  160.   stream: TIvWinSocketStream;
  161.   timeout: Integer): String;
  162. const
  163.   SEGMENT_C = 256;
  164. var
  165.   str: String;
  166.   len, bytesRead: Integer;
  167. begin
  168.   Result := '';
  169.   bytesRead := 0;
  170.   repeat
  171.     if stream.WaitForData(500) then
  172.     begin
  173.       SetLength(str, SEGMENT_C);
  174.       bytesRead := stream.Read(str[1], SEGMENT_C);
  175.       if bytesRead > 0 then
  176.       begin
  177.         SetLength(str, bytesRead);
  178.         Result := Result + str;
  179.       end;
  180.     end;
  181.   until (bytesRead < SEGMENT_C) or (str[SEGMENT_C] = Chr(0));
  182.  
  183.   // All MLTP messages are ended by the Chr(0) character. Removes this.
  184.  
  185.   len := Length(Result);
  186.   if (len > 0) and (Result[len] = Chr(0)) then
  187.     SetLength(Result, len - 1);
  188. end;
  189.  
  190. function TIvServerDictionary.ReadReply(
  191.   timeout: Integer;
  192.   raiseException: Boolean;
  193.   var reply: String): Integer;
  194. var
  195.   index: Integer;
  196. begin
  197.   Result := MLTP_ERROR_C;
  198.   if FStream.WaitForData(timeout) then
  199.   begin
  200.     reply := ReadMessage(FStream, timeout);
  201.     index := Pos(SPACE_C, reply);
  202.     if index > 0 then
  203.     begin
  204.       Result := StrToInt(Copy(reply, 1, index - 1));
  205.       reply := Copy(reply, index + 1, Length(reply));
  206.     end
  207.     else
  208.     begin
  209.       Result := StrToInt(reply);
  210.       reply := '';
  211.     end;
  212.   end
  213.   else if raiseException then
  214.     raise EIvMLTPError.CreateMsg(MLTP_TIMEOUT_C, '');
  215. end;
  216.  
  217. function TIvServerDictionary.Transaction(const msg: String): String;
  218. begin
  219.   TransactionEx(msg, True, Result);
  220. end;
  221.  
  222. function TIvServerDictionary.TransactionEx(
  223.   const msg: String;
  224.   raiseException: Boolean;
  225.   var reply: String): Integer;
  226. begin
  227.   // Writes the message
  228.  
  229.   FStream.Write(msg[1], Length(msg) + 1);
  230.  
  231.   // Reads the reply
  232.  
  233.   Result := ReadReply(FTimeout, IsOpen, reply);
  234.   if raiseException and (Result <> MLTP_OK_C) then
  235.     raise EIvMLTPError.CreateMsg(Result, '');
  236. end;
  237.  
  238. function TIvServerDictionary.GetTranslationCount: Integer;
  239. begin
  240.   Result := StrToInt(Transaction(MLTP_GET_C + SPACE_C + MLTP_TRANSLATIONCOUNT_C));
  241. end;
  242.  
  243. function TIvServerDictionary.GetLanguageCount: Integer;
  244. begin
  245.   Result := StrToInt(Transaction(MLTP_GET_C + SPACE_C + MLTP_LANGUAGECOUNT_C));
  246. end;
  247.  
  248. procedure TIvServerDictionary.GetLanguageData(index: Integer; language: TIvLanguage);
  249. begin
  250.   StringToLanguage(
  251.     Transaction(MLTP_GET_C + SPACE_C + MLTP_LANGUAGEDATA_C + SPACE_C + IntToStr(index)),
  252.     language);
  253. end;
  254.  
  255. function TIvServerDictionary.GetLocaleCount: Integer;
  256. begin
  257.   Result := StrToInt(Transaction(MLTP_GET_C + SPACE_C + MLTP_LOCALECOUNT_C));
  258. end;
  259.  
  260. procedure TIvServerDictionary.GetLocaleData(index: Integer; locale: TIvLocale);
  261. begin
  262.   StringToLocale(
  263.     Transaction(MLTP_GET_C + SPACE_C + MLTP_LOCALEDATA_C + SPACE_C + IntToStr(index)),
  264.     locale);
  265. end;
  266.  
  267. function TIvServerDictionary.TranslateString(
  268.   const str: String;
  269.   var translation: String): Boolean;
  270. var
  271.   resultCode: Integer;
  272. begin
  273.   resultCode := TransactionEx(
  274.     MLTP_TRANSLATE_C + SPACE_C + str,
  275.     False,
  276.     translation);
  277.  
  278.   if resultCode = MLTP_OK_C then
  279.   begin
  280.     Result := translation[1] = '1';
  281.     Delete(translation, 1, 2);
  282.   end
  283.   else
  284.     raise EIvMLTPError.CreateMsg(resultCode, 'Could not translate the string');
  285. end;
  286.  
  287. function TIvServerDictionary.TranslateContextString(
  288.   const str, form, component: String;
  289.   var translation: String): Boolean;
  290. var
  291.   resultCode: Integer;
  292. begin
  293.   resultCode := TransactionEx(
  294.     MLTP_CONTEXT_C + SPACE_C + str + SEPARATOR_C + form + SEPARATOR_C + component,
  295.     False,
  296.     translation);
  297.  
  298.   if resultCode = MLTP_OK_C then
  299.   begin
  300.     Result := translation[1] = '1';
  301.     Delete(translation, 1, 2);
  302.   end
  303.   else
  304.     raise EIvMLTPError.CreateMsg(resultCode, 'Could not translate the string');
  305. end;
  306.  
  307. procedure TIvServerDictionary.TranslateStrings(translations: TList);
  308. var
  309.   i, resultCode: Integer;
  310.   msg, reply: String;
  311.   parser: TIvAnsiParser;
  312.   translation: TIvTranslation;
  313. begin
  314.   // Formats the translation strings
  315.  
  316.   msg := '';
  317.   for i := 0 to translations.Count - 1 do
  318.   begin
  319.     if i > 0 then
  320.       msg := msg + SEPARATOR_C;
  321.     with TIvTranslation(translations[i]) do
  322.       if FContextType = [] then
  323.         msg := msg + Str
  324.       else
  325.         msg := msg + Str + SEPARATOR_C + Form + SEPARATOR_C + Component;
  326.   end;
  327.  
  328.   // Sends the message
  329.  
  330.   if FContextType = [] then
  331.     resultCode := TransactionEx(
  332.       MLTP_TRANSLATE_C + SPACE_C + msg,
  333.       False,
  334.       reply)
  335.   else
  336.     resultCode := TransactionEx(
  337.       MLTP_CONTEXT_C + SPACE_C + msg,
  338.       False,
  339.       reply);
  340.  
  341.   if resultCode = MLTP_OK_C then
  342.   begin
  343.     // Gets translations
  344.  
  345.     parser := TIvAnsiParser.CreateValue(reply, SEPARATOR_C);
  346.     try
  347.       for i := 0 to translations.Count - 1 do
  348.       begin
  349.         translation := TIvTranslation(translations[i]);
  350.         translation.Exists := parser.GetBoolean;
  351.         if translation.Exists then
  352.           translation.Current := parser.GetString;
  353.       end;
  354.     finally
  355.       parser.Free;
  356.     end;
  357.   end
  358.   else
  359.     raise EIvMLTPError.CreateMsg(resultCode, 'Could not translate the strings');
  360. end;
  361.  
  362. procedure TIvServerDictionary.LanguageChanged(languageChanged, localeChanged: Boolean);
  363. begin
  364.   Transaction(MLTP_SET_C + SPACE_C + MLTP_LANGUAGE_C + SPACE_C +
  365.     IntToStr(FActiveLanguage) + SEPARATOR_C +
  366.     IntToStr(FLanguageLocale) + SEPARATOR_C +
  367.     '');
  368.  
  369.   inherited LanguageChanged(languageChanged, localeChanged);
  370. end;
  371.  
  372. class procedure TIvServerDictionary.StringToLanguage(
  373.   const str: String;
  374.   language: TIvLanguage);
  375. var
  376.   parser: TIvAnsiParser;
  377. begin
  378.   parser := TIvAnsiParser.CreateValue(str, SEPARATOR_C);
  379.   try
  380.     language.Primary := parser.GetInteger;
  381.     language.AllSubs := parser.GetString;
  382.     language.DefaultSub := parser.GetInteger;
  383.  
  384.     language.ISOLanguage := parser.GetString;
  385.     language.ISOAllCountries := parser.GetString;
  386.     language.ISODefaultCountry := parser.GetString;
  387.  
  388.     language.CodePage := parser.GetInteger;
  389.     language.EnglishName := parser.GetString;
  390.     language.NativeName := parser.GetString;
  391.     language.FontName := parser.GetString;
  392.     language.FontSize := parser.GetInteger;
  393.     language.OptionsAsInt := parser.GetInteger;
  394.     language.Charset := parser.GetInteger;
  395.  
  396.     language.Init;
  397.   finally
  398.     parser.Free;
  399.   end;
  400. end;
  401.  
  402. class procedure TIvServerDictionary.StringToLocale(
  403.   const str: String;
  404.   locale: TIvLocale);
  405. var
  406.   i: Integer;
  407.   parser: TIvAnsiParser;
  408. begin
  409.   parser := TIvAnsiParser.CreateValue(str, SEPARATOR_C);
  410.   try
  411.     locale.Primary := parser.GetInteger;
  412.     locale.Sub := parser.GetInteger;
  413.     locale.ISOLanguage := parser.GetString;
  414.     locale.ISOCountry := parser.GetString;
  415.     locale.CodePage := parser.GetInteger;
  416.     locale.IsCustom := parser.GetBoolean;
  417.  
  418.     locale.EnglishLanguageName := parser.GetString;
  419.     locale.EnglishCountryName := parser.GetString;
  420.     locale.NativeLanguageName := parser.GetString;
  421.     locale.NativeCountryName := parser.GetString;
  422.     locale.Win16LanguageName := parser.GetString;
  423.     locale.Win16CountryName := parser.GetString;
  424.  
  425.     locale.MeasurementSystem := TIvMeasurementSystem(parser.GetInteger);
  426.     locale.CurrencyString := parser.GetString;
  427.     locale.CurrencyFormat := TIvCurrencyFormat(parser.GetInteger);
  428.     locale.NegCurrFormat := TIvNegativeCurrencyFormat(parser.GetInteger);
  429.     locale.CurrencyDecimals := parser.GetInteger;
  430.     locale.ThousandSeparator := parser.GetChar;
  431.     locale.DecimalSeparator := parser.GetChar;
  432.  
  433.     locale.DateSeparator := parser.GetChar;
  434.     locale.ShortDateFormat := parser.GetString;
  435.     locale.LongDateFormat := parser.GetString;
  436.  
  437.     locale.TimeSeparator := parser.GetChar;
  438.     locale.TimeAMString := parser.GetString;
  439.     locale.TimePMString := parser.GetString;
  440.     locale.TimeLeadingZeros := parser.GetBoolean;
  441.     locale.TimeFormat := TIvTimeFormat(parser.GetInteger);
  442.     locale.TimeMarkPosition := TIvTimeMarkPosition(parser.GetInteger);
  443.  
  444.     locale.CalendarType := TIvCalendarType(parser.GetInteger);
  445.     locale.OptionalCalendarType := TIvCalendarType(parser.GetInteger);
  446.     locale.FirstDayOfWeek := TIvDayOfWeek(parser.GetInteger);
  447.     locale.FirstWeekOfYear := TIvFirstWeekOfYear(parser.GetInteger);
  448.  
  449.     for i := 1 to 12 do
  450.       locale.ShortMonthNames[i] := parser.GetString;
  451.     for i := 1 to 12 do
  452.       locale.LongMonthNames[i] := parser.GetString;
  453.     for i := 1 to 7 do
  454.       locale.ShortDayNames[i] := parser.GetString;
  455.     for i := 1 to 7 do
  456.       locale.LongDayNames[i] := parser.GetString;
  457.  
  458.     // MLTP 1.0
  459.  
  460.     locale.Charset := parser.GetIntegerDef(0);
  461.  
  462.     locale.Init;
  463.   finally
  464.     parser.Free;
  465.   end;
  466. end;
  467.  
  468. class function TIvServerDictionary.GetLoginMessage(
  469.   const userName, password: String;
  470.   clientType: TIvClientType;
  471.   const clientVersion: String;
  472.   application: TIvApplicationType;
  473.   codePage: Integer): String;
  474. begin
  475.   Result := MLTP_LOGIN_C + SPACE_C +
  476.     IntToStr(CURRENT_MLTP_VERSION_C) + SEPARATOR_C +
  477.     userName + SEPARATOR_C +
  478.     password + SEPARATOR_C +
  479.     IntToStr(Integer(clientType)) + SEPARATOR_C +
  480.     clientVersion + SEPARATOR_C +
  481.     IntToStr(Integer(application)) + SEPARATOR_C +
  482.     IntToStr(codePage);
  483. end;
  484.  
  485. procedure TIvServerDictionary.Login;
  486. var
  487.   clientType: TIvClientType;
  488.   clientVersion: String;
  489. begin
  490.   // Opens the connection the dictionary server
  491.  
  492.   try
  493.     FSocket.Open;
  494.     FStream := TIvWinSocketStream.Create(FSocket, FTimeout);
  495.   except
  496.     raise EIvSocketError.Create('Could not make a connection to Dictionary Server at "' + Address + '"');
  497.   end;
  498.  
  499.   clientVersion := '';
  500. {$IFDEF IVVB}
  501.   clientType := ivctVB;
  502. {$ELSE}
  503.   {$IFDEF VER90}
  504.   clientType := ivctDelphi;
  505.   clientVersion := '2';
  506.   {$ELSE}
  507.     {$IFDEF VER93}
  508.   clientType := ivctCBuilder;
  509.   clientVersion := '1';
  510.     {$ELSE}
  511.       {$IFDEF VER100}
  512.   clientType := ivctDelphi;
  513.   clientVersion := '3';
  514.       {$ELSE}
  515.         {$IFDEF VER110}
  516.   clientType := ivctCBuilder;
  517.   clientVersion := '3';
  518.         {$ELSE}
  519.           {$IFDEF VER120}
  520.   clientType := ivctDelphi;
  521.   clientVersion := '4';
  522.           {$ELSE}
  523.             {$IFDEF VER125}
  524.   clientType := ivctCBuilder;
  525.   clientVersion := '4';
  526.             {$ELSE}
  527.   clientType := ivctDelphi;
  528.             {$ENDIF}
  529.           {$ENDIF}
  530.         {$ENDIF}
  531.       {$ENDIF}
  532.     {$ENDIF}
  533.   {$ENDIF}
  534. {$ENDIF}
  535.  
  536.   // version user password clientType clientVersion isApplet codePage
  537.  
  538.   FUserType := TIvUserType(StrToInt(Transaction(GetLoginMessage(
  539.     FUserName,
  540.     FPassword,
  541.     clientType,
  542.     clientVersion,
  543.     ivatApplication,
  544.     FCodePage))));
  545. end;
  546.  
  547. procedure TIvServerDictionary.Logout;
  548. begin
  549.   if FSocket.Connected then
  550.   begin
  551.     try
  552.       Transaction(MLTP_LOGOUT_C);
  553.     except
  554.     end;
  555.     FSocket.Close;
  556.     FStream.Free;
  557.     FStream := nil;
  558.   end;
  559. end;
  560.  
  561. procedure TIvServerDictionary.Open;
  562. var
  563.   reply: String;
  564.   parser: TIvAnsiParser;
  565. begin
  566.   if IsOpen then
  567.     Exit;
  568.  
  569.   // Opens the dictionary
  570.  
  571.   Login;
  572.   reply := Transaction(MLTP_OPEN_C + SPACE_C + FRemoteDictionaryName + SEPARATOR_C + '1');
  573.  
  574.   parser := TIvAnsiParser.CreateValue(reply, SEPARATOR_C);
  575.   try
  576.     parser.GetInteger;
  577.     parser.GetInteger;
  578.     FContextType := TIvContext.ContextCodeToType(TIvContextCode(parser.GetInteger));
  579.   finally
  580.     parser.Free;
  581.   end;
  582.  
  583.   inherited Open;
  584. end;
  585.  
  586. procedure TIvServerDictionary.Close;
  587. begin
  588.   if IsOpen then
  589.   begin
  590.     try
  591.       Transaction(MLTP_CLOSE_C);
  592.     except
  593.     end;
  594.     Logout;
  595.   end;
  596.  
  597.   inherited Close;
  598. end;
  599.  
  600. function TIvServerDictionary.GetDictionaries(names, owners, descriptions: TStrings): Integer;
  601. var
  602.   name, owner, description: String;
  603.   parser: TIvAnsiParser;
  604. begin
  605.   Result := 0;
  606.   parser := TIvAnsiParser.CreateValue(
  607.     Transaction(MLTP_GET_C + SPACE_C + MLTP_DICTIONARIES_C),
  608.     SEPARATOR_C);
  609.   while not parser.Eol do
  610.   begin
  611.     name := parser.GetString;
  612.     if names <> nil then
  613.       names.Add(name);
  614.  
  615.     name := parser.GetString;
  616.     if owners <> nil then
  617.       owners.Add(owner);
  618.  
  619.     name := parser.GetString;
  620.     if descriptions <> nil then
  621.       descriptions.Add(description);
  622.  
  623.     Inc(Result);
  624.   end;
  625.   parser.Free;
  626. end;
  627.  
  628. function TIvServerDictionary.GetTranslationMode: TIvTranslationMode;
  629. begin
  630.   Result := FTranslationMode;
  631. end;
  632.  
  633. end.
  634.